

#import "WHDataCache.h"
#import "NSString+WHMD5.h"

@implementation WHDataCache

static WHDataCache *cache = nil;
+(WHDataCache *)sharedDataCache{
    @synchronized(self){
        if (cache == nil) {
            //            cache = [[WHDataCache alloc]init];
            cache = [[[WHDataCache class] alloc]init];
        }
        
    }
    return cache;
}

- (id)init {
    if (self = [super init]) {
        _interval = 10*60;//10分钟
    }
    return self;
}

+(id)allocWithZone:(struct _NSZone *)zone{
    if (cache == nil) {
        cache = [super allocWithZone:zone];
    }
    return cache;
}

//保存数据
- (void)saveData:(NSData *)data andNameString:(NSString *)path {
//    DLog(@"%@", NSHomeDirectory());
    NSString *myPath = [NSString stringWithFormat:@"%@/Documents/DataCache/", NSHomeDirectory()];
    NSFileManager *manager = [NSFileManager defaultManager];
    
    //创建当前的路径
    BOOL isSuc = [manager createDirectoryAtPath:myPath withIntermediateDirectories:YES attributes:nil error:nil];
    
    if (!isSuc) {
        DLog(@"创建路径失败");
        return;
    }
    
    //通过MD5对文件的名字进行加密
    //MD5简介:原理:将不同长度的字符串通过MD5加密转换成等长的16进制的字符串(24,32).由于位数很大,可以保证数据的唯一性,进而保证数据的安全
    
    NSString *MD5Path = [path MD5Hash];
    
    NSString *pathFile = [NSString stringWithFormat:@"%@%@", myPath, MD5Path];
    
    //将数据写入指定的路径的文件
    [data writeToFile:pathFile atomically:YES];
    
}

//取数据
- (NSData *)getDataWithNameString:(NSString *)path {
    NSString *MD5Path = [path MD5Hash];
    
    NSString *myPath = [NSString stringWithFormat:@"%@/Documents/DataCache/%@", NSHomeDirectory(), MD5Path];
    
    NSFileManager *fileManger = [NSFileManager defaultManager];
    //判断当前的文件是否存在cun'zai
    BOOL isEx = [fileManger fileExistsAtPath:myPath];
    
    if (!isEx) {
        return nil;
    }
    
    //判断文件是否失效(是否在规定的时间内)
    
//    BOOL isValude = [[[self getLastDate:myPath] addTimeInterval:_interval] isEqualToDate:[NSDate date]];
    
    NSTimeInterval timeInt = [[NSDate date] timeIntervalSinceDate:[self getLastDate:myPath]];
    
    if (timeInt >= _interval) {
        return nil;
    }
    
    //取数据
    NSData *data = [NSData dataWithContentsOfFile:myPath];
    
    return data;
    
}

- (NSDate *)getLastDate:(NSString *)myPath {
    //判断文件是否失效(是否在规定的时间内)
    NSFileManager *manager = [NSFileManager defaultManager];
    NSDictionary *dic = [manager attributesOfItemAtPath:myPath error:nil];
//    DLog(@"%@", dic);
    /**
     {
     NSFileCreationDate = "2015-04-24 03:21:37 +0000";
     NSFileExtensionHidden = 0;
     NSFileGroupOwnerAccountID = 20;
     NSFileGroupOwnerAccountName = staff;
     NSFileModificationDate = "2015-04-24 03:21:37 +0000";
     NSFileOwnerAccountID = 501;
     NSFilePosixPermissions = 420;
     NSFileReferenceCount = 1;
     NSFileSize = 17089;
     NSFileSystemFileNumber = 1880269;
     NSFileSystemNumber = 16777217;
     NSFileType = NSFileTypeRegular;
     }
     */
    
    //    NSDate *lastDate =
    
    //最后访问文件的时间
    return dic[@"NSFileModificationDate"];
}

- (void)deleteDataWithNameString:(NSString *)path {
    NSString *MD5Path = [path MD5Hash];
    NSString *cachePath = [NSString stringWithFormat:@"%@/Documents/DataCache/%@", NSHomeDirectory(), MD5Path];
    
    NSFileManager *fileManger = [NSFileManager defaultManager];
    BOOL isEx = [fileManger fileExistsAtPath:cachePath];
    
    if (isEx) {
        [fileManger removeItemAtPath:cachePath error:nil];
    }
    
}



@end
